/*
 * Copyright (c) 2010-2011 NOO. All Rights Reserved.
 * [Id:OpertionService.java  11-10-6 下午5:03 poplar.yfyang ]
 */
package org.mumu.pawn.cmps.def;

import org.jboss.logging.Logger;
import org.joda.time.DateTimeUtils;
import org.mumu.pawn.beans.common.AccountSessionBean;
import org.mumu.pawn.cmps.OperateCmp;
import org.mumu.pawn.domain.Operate;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.util.List;

/**
 * <p>
 * 操作请求服务.
 * </p>
 *
 * @author poplar.yfyang
 * @version 1.0 11-10-6 下午5:03
 * @since JDK 1.5
 */
@SuppressWarnings("CdiInjectionPointsInspection")
public class DefOperateCmp implements OperateCmp {
    private static final long serialVersionUID = 4456188435714153623L;


    /**
     * 获取所有的操作信息以及所属模块
     *
     * @return 所有的操作信息以及所属的模块
     */
    public List<Operate> listOperate() {
        return entityManager.createQuery("from Operate o order by o.sort").getResultList();
    }

    @Override
    public void saveOrUpdateOperate(Operate operate) {
        if (operate.getId() == 0) {
            log.info("新增：" + operate.getOperateName());
            operate.setCreateTime(DateTimeUtils.currentTimeMillis());
            operate.setCreateActor(accountSessionBean.getAccount().getId());
            entityManager.persist(operate);
        } else {
            log.info("更新：" + operate.getOperateName());
            operate.setUpdateTime(DateTimeUtils.currentTimeMillis());
            operate.setUpdateActor(accountSessionBean.getAccount().getId());
            entityManager.merge(operate);
        }
    }

    @Override
    public void deleteOperate(long id) {
        Operate operate = findOperate(id);
        if (operate != null) {
            entityManager.remove(operate);
        }
    }

    @Override
    public Operate findOperate(long id) {
        return entityManager.find(Operate.class, id);
    }


    @Inject
    private EntityManager entityManager;
    @Inject
    private Logger log;

    @Inject
    private AccountSessionBean accountSessionBean;

}
